package cn.com.czpb.client.module.personal.dialog;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.TextView;

import cn.com.czpb.client.R;
import cn.com.czpb.client.model.event.ChangeNameEvent;
import cn.com.czpb.client.model.usercenter.UserEntity;
import cn.com.czpb.client.module.personal.WodeFragment;
import cn.com.czpb.client.net.Factory;
import cn.com.czpb.client.utils.LogUtils;
import cn.com.czpb.client.utils.ParamUtils;
import cn.com.czpb.client.utils.SPUtil;
import cn.com.czpb.client.utils.TUtils;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import de.greenrobot.event.EventBus;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;
import rx.functions.Func1;
import rx.schedulers.Schedulers;

public class ChangeNameOrSignDialog extends Dialog implements View.OnClickListener {

    @BindView(R.id.changname_edit)
    EditText changname_edit;
    EditText changint_edit;
    @BindView(R.id.changename_title)
    TextView changename_title;
    TextView changename_confirm;
    TextView changename_cancel;

    private Activity activity;
    private View view;
    private SPUtil spu;

    private int type;

    public ChangeNameOrSignDialog(Context context) {
        super(context);
        activity = (Activity) context;
        spu = SPUtil.getInstance();

    }


    public ChangeNameOrSignDialog(Context context, int theme, int type) {
        super(context, theme);
        activity = (Activity) context;
        spu = SPUtil.getInstance();

        this.type = type;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        init();
    }


    private void init() {
        WindowManager wm = (WindowManager) activity.getSystemService(Context.WINDOW_SERVICE);
        int width = wm.getDefaultDisplay().getWidth();
        LogUtils.i("width-->" + width);
        view = LayoutInflater.from(activity).inflate(R.layout.dialog_changename, null);

        setContentView(view);
        changname_edit = view.findViewById(R.id.changname_edit);
        changint_edit = view.findViewById(R.id.changint_edit);
        if (type == 0) {
            changname_edit.setVisibility(View.VISIBLE);
            changint_edit.setVisibility(View.GONE);
        } else if (type == 1) {
            changint_edit.setVisibility(View.VISIBLE);
            changname_edit.setVisibility(View.GONE);
        }
        changename_title = view.findViewById(R.id.changename_title);
        changename_confirm = view.findViewById(R.id.changename_confirm);
        changename_cancel = view.findViewById(R.id.changename_cancel);
        changename_cancel.setOnClickListener(this);
        changename_confirm.setOnClickListener(this);
        Window dialogWindow = getWindow();
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        dialogWindow.setGravity(Gravity.END | Gravity.BOTTOM);
        lp.width = width;
        dialogWindow.setAttributes(lp);
        if (null != spu.getUser()) {
            if (type == 0) {
                changename_title.setText("请输入昵称(最多8个字)");
                changname_edit.setText(spu.getUser().getNickname());



            } else {
                changename_title.setText("请输入个人介绍(最多200个字)");
                changint_edit.setText(spu.getUser().getDesc());
            }
        }

    }

//	@OnClick({R.id.changename_confirm, R.id.changename_cancel})
//	public void click(View view) {
//		switch (view.getId()) {
//			case R.id.changename_confirm:
//				changeName();
//				break;
//			case R.id.changename_cancel:
//				dismiss();
//				break;
//			default:
//				break;
//		}
//	}

    public void changeName(String str) {

        String str1 = changint_edit.getText().toString();

        Map<String, String> map = new HashMap<>();
        if (0 == type) {
            map.put("nickname", str);
        } else {
            map.put("desc", str);
        }
        map.put("uid", spu.getUser().getUid());

        Factory.provideHttpService()
                .changeUser(ParamUtils.getRequestParam(map))
                .onBackpressureBuffer()
                .subscribeOn(Schedulers.io())
                .filter(new Func1<UserEntity, Boolean>() {
                    @Override
                    public Boolean call(UserEntity userEntity) {
                        if ("200".equals(userEntity.code)) {
                            LogUtils.e("nickname---" + spu.getUser().getNickname());

                            return true;
                        } else {
                            TUtils.toast(userEntity.msg);
                        }
                        return false;
                    }
                })
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Action1<UserEntity>() {
                    @Override
                    public void call(UserEntity entity) {
                        spu.setUser(entity.data);
                        Intent intent = new Intent();
                        intent.setAction(WodeFragment.ACTION_USER);
                        activity.sendBroadcast(intent);
                        dismiss();

                    }
                }, new Action1<Throwable>() {
                    @Override
                    public void call(Throwable throwable) {

                        LogUtils.i("200-->失败" + throwable.toString());
                    }
                });


    }


    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.changename_confirm:
                if (type == 0) {
                    String str = changname_edit.getText().toString();
                    if (null == str || "".equals(str)) {
                        TUtils.toast("请输入昵称");
                        return;
                    }
                    changeName(str);
                }else if(type == 1) {
                    String str = changint_edit.getText().toString();
                    if (null == str || "".equals(str)) {
                        TUtils.toast("请输入个人介绍");
                        return;
                    }
                    changeName(str);
                }
                break;
            case R.id.changename_cancel:
                dismiss();
                break;
            default:
                break;
        }
    }

    @Override
    public void dismiss() {
        super.dismiss();
        ChangeNameEvent event = new ChangeNameEvent();
        event.setNameChange(true);
        EventBus.getDefault().post(event);
    }

}